package org.example.booklist.Controller;

import ch.qos.logback.core.encoder.EchoEncoder;
import ch.qos.logback.core.util.StringUtil;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.example.booklist.constant.Constants;
import org.example.booklist.enums.ResultStatus;
import org.example.booklist.model.*;
import org.example.booklist.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.print.Book;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;


    @RequestMapping("/getBookList")
    public List<BookInfo> getBookList() {
        List<BookInfo> bookInfos = new ArrayList<>();
        //BookService bookService = new BookService();
        return bookService.getBookList();
    }

    //添加图书：
    @RequestMapping("/addBook")
    public String addBook(BookInfo bookInfo){
        log.info("接收参数: bookName={}, author={}, count={}, price={}, publish={}, status={}",
                bookInfo.getBookName(), bookInfo.getAuthor(), bookInfo.getCount(),
                bookInfo.getPrice(), bookInfo.getPublish(), bookInfo.getStatus());
        //校验参数：
        log.info("添加图书：接收到参数：bookInfo={}",bookInfo);
        if(!StringUtils.hasLength(bookInfo.getBookName())
           || !StringUtils.hasLength(bookInfo.getAuthor())
           || bookInfo.getCount() == null
           || bookInfo.getPrice() == null
           || !StringUtils.hasLength(bookInfo.getPublish())
           || bookInfo.getStatus() == null){
            return "输入参数不合法";
        }
        //添加图书
        try{
            Integer result = bookService.insertBook(bookInfo);
            if(result > 0){
                return "";
            }
        }catch (Exception e) {
            e.printStackTrace();
            return "添加失败: " + e.getMessage(); // 返回错误信息
        }
        return "添加失败";
    }

    //查询图书列表，使用分页的操作

    @RequestMapping("/getBookListByPage")
    public Result<PageResult<BookInfo>>  getBookListPage(PageRequest pageRequest, HttpSession httpSession){
       //从session中获取到用户信息
        // 如果用户信息为空，那么说明用户未登录：
        //下面从session中获取到用户信息：
      /* UserInfo loginUserInfo =  (UserInfo) httpSession.getAttribute(Constants.USER_SESSION_KEY);
       if(loginUserInfo == null || loginUserInfo.getId() < 0){
          return Result.nologin();
       }*/

       //用户不为空：
        PageResult<BookInfo>  bookList = bookService.getBookListPage(pageRequest);
        return Result.success(bookList);
    }


    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        log.info("根据ID查询图书信息："+bookId);
        return bookService.queryBookById(bookId);
    }

    //更新图书：
    @RequestMapping("/updateBook")
    public String updateBook(BookInfo bookInfo){
        log.info("更新图书：bookInfo:{}",bookInfo);
        try{
            bookService.updateBookById(bookInfo);
            return "";
        }catch (Exception e){
            log.error("更新图书失败：e :"+e);
            return "更新图书失败:"+e.getMessage();
        }
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(Integer bookId){
        log.info("删除图书： bookId：{}",bookId);
        try{
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(0);
            Integer result = bookService.updateBookById(bookInfo);
            if(result > 0){
                return "";
            }
            return "内部错误，请联系管理员";
        }catch (Exception e){
            log.error("删除图书失败：e:",e);
            return "内部错误，请联系管理员";
        }
    }

    //批量删除图书：
    @RequestMapping("/batchDeleteBook")
    public String batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书：ids:{}",ids);
        Integer result = bookService.batchDeleteBookByIds(ids);
        if(result > 0){
            return "";
        }
        return "批量删除失败，请联系管理员";
    }
}
